home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2001 September / PC-WELT 9-2001.ISO / software / hw / brennen / flask_src.exe / Misc / CArray.h < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-07  |  3.1 KB  |  163 lines

  1. /* 
  2.  *  CArray.h
  3.  *
  4.  *    Copyright (C) Alberto Vigata - January 2000 - ultraflask@yahoo.com
  5.  *
  6.  *  This file is part of FlasKMPEG, a free MPEG to MPEG/AVI converter
  7.  *    
  8.  *  FlasKMPEG is free software; you can redistribute it and/or modify
  9.  *  it under the terms of the GNU General Public License as published by
  10.  *  the Free Software Foundation; either version 2, or (at your option)
  11.  *  any later version.
  12.  *   
  13.  *  FlasKMPEG is distributed in the hope that it will be useful,
  14.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  *  GNU General Public License for more details.
  17.  *   
  18.  *  You should have received a copy of the GNU General Public License
  19.  *  along with GNU Make; see the file COPYING.  If not, write to
  20.  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
  21.  *
  22.  */
  23.  
  24.  
  25. // THIS IS A TEMPLATED CLASS. ALL CODE IS INCLUDED HERE
  26. #ifndef CARRAY_H
  27. #define CARRAY_H
  28.  
  29. #include <malloc.h>
  30. #include <memory.h> 
  31. template <class T> class CArr
  32. {
  33.  
  34.     public:    
  35.         CArr();
  36.         ~CArr();
  37.         T  *GetData();
  38.         int GetCount();
  39.         int AddItem(T *item);
  40.         int EmptyArray();
  41.         int SetArraySize(int size);
  42.         T& operator[](int index);
  43.         CArr<T>& operator=(CArr<T>& right);
  44.     private:
  45.         int n_count;
  46.         T   *data;
  47. };
  48.  
  49.  
  50.  
  51.  
  52. template <class T> CArr<T>::CArr()
  53. {
  54.     n_count = 0;
  55.     data    = 0;
  56. };
  57.  
  58. template <class T> CArr<T>::~CArr()
  59. {
  60.     if(data)
  61.         delete []data;
  62.     data = 0;
  63. };
  64.  
  65.  
  66. template <class T> int CArr<T>::AddItem(T *item)
  67. {
  68.     T *temp;
  69.     int i;
  70.     if(!item)
  71.         return 0;
  72.     // first allocate space for the current data plus one item
  73.     temp = new T[n_count + 1];
  74.     if(!temp)
  75.         return 0;
  76.     // Copy all contents
  77.     //   Current contents first
  78.     for(i=0; i<n_count; i++)
  79.          temp[i] = data[i];
  80.     // copy item to add
  81.     temp[n_count] = *item;
  82.  
  83.     // delete previous vector. delete will call the destructors if any?
  84.     if(data)
  85.         delete []data;
  86.     data = temp;
  87.  
  88.     n_count++;
  89.     return 1;
  90. };
  91.  
  92. template <class T> T* CArr<T>::GetData()
  93. {
  94.     return data;
  95. };
  96.  
  97. template <class T> int CArr<T>::EmptyArray()
  98. {
  99.     if(data)
  100.         delete []data;
  101.     data    = 0;
  102.     n_count = 0;
  103.     return 1;
  104. };
  105.  
  106. template <class T> int CArr<T>::SetArraySize(int size)
  107. {
  108.     T *temp;
  109.     int i;
  110.     if(size<=0)
  111.         return 0;
  112.  
  113.     // first allocate space for data
  114.     temp = new T[size];
  115.     if(!temp)
  116.         return 0;
  117.     // Copy all current contents
  118.     for(i=0; i<(n_count > size ? size : n_count); i++)
  119.          temp[i] = data[i];
  120.     
  121.  
  122.     // delete previous vector. delete will call the destructors if any?
  123.     if(data)
  124.        delete []data;
  125.     data = temp;
  126.     if(!data)
  127.         return 0;
  128.  
  129.     n_count = size;
  130.     return 1;
  131. };
  132.  
  133.  
  134. template <class T> T& CArr<T>::operator[](int index)
  135. {
  136.     if(index>n_count-1)
  137.         return data[n_count-1];
  138.     if(index<0)
  139.         return data[0];
  140.     return data[index];
  141. };
  142.  
  143. template <class T> CArr<T>& CArr<T>::operator=(CArr<T>& right)
  144. {
  145.     int i,count;
  146.     //Copy right array
  147.     EmptyArray();
  148.     count = right.GetCount();
  149.     SetArraySize( count );
  150.     for(i=0; i<count; i++)
  151.         data[i]=right[i];
  152.  
  153.     return *this;
  154. };
  155.  
  156. template <class T> int CArr<T>::GetCount()
  157. {
  158.     return n_count;
  159. };
  160.  
  161.  
  162.  
  163. #endif